-- UISkyInstanceInfo
-- Created by chengb Jan/22/2016
-- 天空副本信息界面

UISkyInstanceInfo = class("UISkyInstanceInfo", function()
    return cc.Layer:create();
end);

function UISkyInstanceInfo.create(dungeonId)
    return UISkyInstanceInfo.new(dungeonId);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 构造函数
function UISkyInstanceInfo:ctor(dungeonId)
    self:setName("UISkyInstanceInfo");

    -- 记录迷宫编号
    self.dungeonId = dungeonId;

    -- 初始化
    local node = cc.CSLoader:createNode("layout/sky_city/SkyInstanceInfo.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UISkyInstanceInfo:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UISkyInstanceInfo:redraw()
    -- 显示名称
    local name = DungeonAreaM.query(self.dungeonId, "name");
    local nameLabel = findChildByName(self.node, "CT/name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BLUE);
    nameLabel:setString(name);

    -- 获取描述
    local desc = DungeonAreaM.query(self.dungeonId, "desc");
    local eventId = SkyInstanceM.query(self.dungeonId, "event_id");
    local appearDesc = SkyInstanceM.query(self.dungeonId, "appear_desc");

    -- 替换探索等级
    local requireLevel = SkyExploreM.query(eventId, "require_level");
    appearDesc = string.gsub(appearDesc, "{explore_level}", tostring(requireLevel));

    -- 替换探索次数
    local requireTimes = SkyExploreM.query(eventId, "require_times");
    appearDesc = string.gsub(appearDesc, "{explore_times}", tostring(requireTimes));

    -- 替换最高层数
    local maxLayer = DungeonAreaM.query(self.dungeonId, "max_floor");
    appearDesc = string.gsub(appearDesc, "{max_layer}", tostring(maxLayer));

    -- 显示描述
    local descStr = string.format("%s\n\n[blue]%s[-]", desc, appearDesc);
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, descStr);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL,
        { nameLabel, descLabel, });
end

-- 注册点击事件
function UISkyInstanceInfo:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UISkyInstanceInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册背景点击事件
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UISkyInstanceInfo");
        end
    end
    bgImg:addTouchEventListener(onBgClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 注册事件处理回调函数
function UISkyInstanceInfo:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UISkyInstanceInfo 界面析构清理");
        end
    end);
end

